/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.javacommunity.controller.bean;

import com.javacommunity.model.dao.AnswerVotationDAO;
import com.javacommunity.model.dao.PostVotationDAO;
import com.javacommunity.model.dao.QuestionVotationDAO;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;

import com.javacommunity.model.dao.RoleDAO;
import com.javacommunity.model.dao.UserDAO;
import com.javacommunity.model.dao.impl.AnswerVotationDAOImpl;
import com.javacommunity.model.dao.impl.PostVotationDAOImpl;
import com.javacommunity.model.dao.impl.QuestionVotationDAOImpl;
import com.javacommunity.model.dao.impl.RoleDAOImpl;
import com.javacommunity.model.dao.impl.UserDAOImpl;
import com.javacommunity.model.entity.Answer;
import com.javacommunity.model.entity.Post;
import com.javacommunity.model.entity.Question;
import com.javacommunity.model.entity.Role;
import com.javacommunity.model.entity.User;
import com.javacommunity.util.Support;
import java.util.Date;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;


/**
 *
 * @author Cong Pham Dinh
 */
@ManagedBean
@RequestScoped
public class UserManagement {

    private final UserDAO userDAO = UserDAOImpl.getInstance();
    private final RoleDAO roleDAO = RoleDAOImpl.getInstance();
    private final PostVotationDAO postVotationDAO = PostVotationDAOImpl.getInstance();
    private final QuestionVotationDAO questionVotationDAO = QuestionVotationDAOImpl.getInstance();
    private final AnswerVotationDAO answerVotationDAO = AnswerVotationDAOImpl.getInstance();
    
    private User user;
    
    public String createUser(ActionEvent event) {
        String page = "/pages/register.jsf";
        String msg;

        User newUser = new User();
        
        newUser.setUserName(user.getUserName());
        newUser.setPwd(Support.encryptMD5(user.getPwd()));
        newUser.setFullName(user.getFullName());
        newUser.setDayOfBirth(user.getDayOfBirth());
        newUser.setGender(user.isGender());
        newUser.setEmail(user.getEmail());
        newUser.setPhoneNumber(user.getPhoneNumber());

        String imagePath;
        if (user.isGender()) {
            imagePath = "images/avatar/avatar_male.jpg";
        } else {
            imagePath = "images/avarar/avatar_female.jpg";
        }
        newUser.setAvatarPath(imagePath);
        Role r = roleDAO.getRoleById(3);
        newUser.setRole(r);
        String idActive = Support.encryptMD5(newUser.getUserName() + new Date().toString());
        if (!userDAO.checkUserName(newUser.getUserName())) {
            if (!userDAO.checkEmail(newUser.getEmail())) {
                if (userDAO.createUser(newUser)) {
                    Support.sendMail(newUser.getEmail(), idActive);
                    msg = "Bạn đã tạo account thành công!";
                    page = "/pages/main.jsf?faces-redirect=true";
                } else {
                    msg = "Account create failed!";
                }
            } else {
                msg = "Địa chỉ Email đã tồn tại! Vui lòng chọn lại email khác!";
            }
        } else {
            msg = "Tên đăng nhập đã có người sử dụng, vui lòng chọn lại! ";
        }
        this.addMessages(msg);
        return page;
    }

    public int getNumberOfVotes(User user){
        int numberOfVotes = 0;
        for (Post post : user.getPosts()) {
            numberOfVotes += postVotationDAO.countVotation(post);
        }
        for(Question question: user.getQuestions()){
            numberOfVotes += questionVotationDAO.countVotation(question);
        }
        for (Answer answer : user.getAnswers()) {
            numberOfVotes += answerVotationDAO.countVotation(answer);
        }
        return numberOfVotes;
    }    
    public void addMessages(String msg) {
        FacesMessage fm = new FacesMessage(FacesMessage.SEVERITY_INFO, msg, "");
        FacesContext.getCurrentInstance()
                .addMessage(null, fm);
    }

    public User getUser() {
        if (user == null) {
            user = new User();
        }
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }
    
}
